package com.pai4j.ugc.service;

import com.pai4j.domain.dto.llm.*;
import com.pai4j.domain.vo.llm.MoonShotAIClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

/**
 *  AI 服务
 *
 * @author: CYM-pai
 * @date: 2025/05/05 14:56
 **/
@Service
public class MoonshotAIService {

    /**
     * Moonshot api Key
     */
    @Value("${moonshot.api.key}")
    private String apiKey;

    /**
     * Moonshot api model
     */
    @Value("${moonshot.api.model}")
    private String model;

    
    
    /**
     *   问答接口
     *
     * @author: CYM-pai
     * @date: 2025/07/14 17:43
     **/
    public ChatCompletionResponse chat(List<ChatCompletionMessage> messageList) throws Exception {
        MoonShotAIClient client = new MoonShotAIClient(apiKey);
        final List<ChatCompletionMessage> messages = messageList;
        return client.ChatCompletion(new ChatCompletionRequest(
                model,
                messages,
                4096,
                0.6f,
                1
        ));
    }
    
    /**
     *   流式问答接口
     *
     * @author: CYM-pai
     * @date: 2025/07/14 17:43
     **/
    public String chat(SseEmitter emitter, List<ChatCompletionMessage> messageList) throws Exception {
        
        StringBuilder aiMessage = new StringBuilder();
        
        MoonShotAIClient client = new MoonShotAIClient(apiKey);
        
        
        final List<ChatCompletionMessage> messages = messageList;
        try {
            client.ChatCompletionStream(new ChatCompletionRequest(
                    model,
                    messages,
                    2000,
                    0.3f,
                    1
            )).subscribe(
                    streamResponse -> {
                        if (streamResponse.getChoices().isEmpty()) {
                            return;
                        }
                        for (ChatCompletionStreamChoice choice : streamResponse.getChoices()) {
                            String finishReason = choice.getFinishReason();
                            if (finishReason != null) {
                                emitter.send("finished");
                                continue;
                            }
                            String responseContent = choice.getDelta().getContent();
                            if (responseContent != null) {
                                responseContent = responseContent.replaceAll("\n", "<br>");
                                System.out.println(responseContent);
                                emitter.send(responseContent);
                                aiMessage.append(responseContent);
                            }
                        }
                    },
                    error -> {
                        error.printStackTrace();
                    },
                    () -> {
//                        emitter.complete(); // 完成事件流发送
                    }
            );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aiMessage.toString();
    }
}